<?php
/**
 * Created by PhpStorm.
 * User: jack
 * Date: 2020/1/30
 * Time: 上午12:57
 */
namespace app\job;

use app\api\server\Zt;
use think\queue\Job;
use think\facade\Db;

class ZtOrder
{
    //nohup php think queue:listen --queue ZtOrder &

    public function fire(Job $job, $data)
    {
        // 具体执行业务
        $isJobDone = $this->doJob($data);
        if ($isJobDone["code"] == 200) {
            // 消息队列执行成功，删除队列，否则会一直执行
            $this->finish($data,$isJobDone);
            $job->delete();
        } else {
            // 消息队列执行失败
            // 获取消息队列已经重试了几遍
            $attempts = $job->attempts();
            if ($attempts > 3) {
                $job->delete();
            }else{
                $job->release(200);
            }
        }

    }

    // 消息队列执行方法
    public function doJob($data)
    {
        return (new Zt())->report($data["third_order_no"]);
    }


    //完成订单
    public function finish($data,$isJobDone)
    {
        DB::table("zl_survey_report")
            ->where("third_order_no",$data["third_order_no"])
            ->update(["report_detail"=>json_encode($isJobDone["data"],302)]);
        //完成订单
        DB::table("zl_survey_order")
            ->where("order_no",$data["order_no"])
            ->update(["status"=>30]);
    }


}